<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Http;
use think\Db;
use think\helper\Time;
use think\Request;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        return $this->view->fetch();
    }

    //获得时间
    public function time($num1, $num2, $type = 'minutes')
    {
        $t=date('Y-m-d H:i',time());
        $s=date('s',time());
        return [strtotime("$num1 $type -$s seconds"), strtotime("$num2 $type -$s seconds")];
//        return [date('Y-m-d H:i:s',strtotime("$num1 $type -$s seconds")),date('Y-m-d H:i:s', strtotime("$num2 $type -$s seconds"))];
    }
    public function timeHours($num1, $num2, $type = 'hours')
    {
        $t=date('Y-m-d H:i');
        $m=date('i');
        return [strtotime("$t $num1 $type -$m minutes"), strtotime("$t $num2 $type -$m minutes")];
//        return [date('Y-m-d H:i:s',strtotime("$t $num1 $type -$m minutes")), date('Y-m-d H:i:s',strtotime("$t $num2 $type -$m minutes"))];
    }

    //1分钟 五分钟  10分钟  1小时   4小时  1天
    public function minuteDataOne()
    {
        $i = 0;
        $j = 1; //间隔分钟
        $time_ago = 20; //取过去20分钟
        do {
            $i = $i - $j;
            $array[] = [$i, $i + 1];
        } while ($i >= -$time_ago);
        foreach ($array as $index => $v) {
            $timeWhere[$index] = $this->time($v[0], $v[1]);
        }
        foreach ($timeWhere as $index => $item) {
            $data = Db::name('huobi')->whereTime('create_time', 'between', $timeWhere[$index])->order('create_time desc')->find();
            if (is_array($data)) {
                $arr[] = $data;
            }
        }
        foreach ($arr as $index => $item) {
            $arr[$index]['create_time'] = '"' . date('Y-m-d H:i:s', $item['create_time']) . '"';
        }
        $returnData = [
            'c_last' => array_column($arr, 'c_last'),
            'u_last' => array_column($arr, 'u_last'),
            'closing_price' => array_column($arr, 'closing_price'),
            'last_price' => array_column($arr, 'last_price'),
            'ltp' => array_column($arr, 'ltp'),
            'create_time' => array_column($arr, 'create_time'),
        ];
        return json($returnData);
    }
    public function minuteDataFive()
    {
        $i = 0;
        $j = 5; //间隔分钟
        $time_ago = 180; //取过去20分钟
        do {
            $i = $i - $j;
            $array[] = [$i, $i + 5];
        } while ($i >= -$time_ago);
        foreach ($array as $index => $v) {
            $timeWhere[$index] = $this->time($v[0], $v[1]);
        }
//        halt($timeWhere);
        foreach ($timeWhere as $index => $item) {
            $data = Db::name('huobi')->whereTime('create_time', 'between', $timeWhere[$index])->order('create_time desc')->find();
            if (is_array($data)) {
                $arr[] = $data;
            }
        }
        foreach ($arr as $index => $item) {
            $arr[$index]['create_time'] = '"' . date('Y-m-d H:i:s', $item['create_time']) . '"';
        }
        $returnData = [
            'c_last' => array_column($arr, 'c_last'),
            'u_last' => array_column($arr, 'u_last'),
            'closing_price' => array_column($arr, 'closing_price'),
            'last_price' => array_column($arr, 'last_price'),
            'ltp' => array_column($arr, 'ltp'),
            'create_time' => array_column($arr, 'create_time'),
        ];
        return json($returnData);
    }
    public function minuteDataTen()
    {
//        $a = 10;//间隔分钟
//        $b = time() - 5 * 3600; //去小时之前
//        $timeWhere = [
//            $this->time(-10, -1),
//            $this->time(-20, -11),
//            $this->time(-30, -21),
//            $this->time(-40, -31),
//            $this->time(-50, -41),
//            $this->time(-60, -51),
//            $this->time(-70, -61),
//            $this->time(-80, -71),
//            $this->time(-90, -81),
//            $this->time(-100, -91),
//            $this->time(-110, -101),
//            $this->time(-120, -111),
//            $this->time(-130, -121),
//            $this->time(-140, -131),
//            $this->time(-150, -141),
//            $this->time(-160, -151),
//            $this->time(-170, -161),
//            $this->time(-180, -171),
//            $this->time(-190, -181),
//        ];
//        dump($timeWhere);
        $i = 0;
        $j = 10; //间隔分钟
        $time_ago = 180; //取过去两个小时
        do {
            $i = $i - $j;
            $array[] = [$i, $i + 9];
        } while ($i >= -$time_ago);
        foreach ($array as $index => $v) {
            $timeWhere[$index] = $this->time($v[0], $v[1]);
        }
        foreach ($timeWhere as $index => $item) {
            $data = Db::name('huobi')->whereTime('create_time', 'between', $timeWhere[$index])->order('create_time desc')->find();
            if (is_array($data)) {
                $arr[] = $data;
            }
        }
//        $arr=[$arr];
//         dump(array_column($arr, 'c_last'));
//        $arr = Db::query("SELECT * FROM fa_huobi WHERE (CEILING(MINUTE(FROM_UNIXTIME(create_time)) / $a) = MINUTE(FROM_UNIXTIME(create_time)) / $a
//        OR FLOOR(MINUTE(FROM_UNIXTIME(create_time)) / $a) = MINUTE(FROM_UNIXTIME(create_time)) / $a)
//        AND  create_time>$b limit 60");
        foreach ($arr as $index => $item) {
            $arr[$index]['create_time'] = '"' . date('Y-m-d H:i:s', $item['create_time']) . '"';
        }
        $returnData = [
            'c_last' => array_column($arr, 'c_last'),
            'u_last' => array_column($arr, 'u_last'),
            'closing_price' => array_column($arr, 'closing_price'),
            'last_price' => array_column($arr, 'last_price'),
            'ltp' => array_column($arr, 'ltp'),
            'create_time' => array_column($arr, 'create_time'),
        ];
        return json($returnData);
    }

    //间隔1小时的数据
    public function hoursData()
    {
//        $b = strtotime('-3 day'); //去小时之前
//        $arr = Db::query("SELECT *
//    FROM
//      fa_huobi
//    WHERE MINUTE(FROM_UNIXTIME(create_time))=0
//    AND  create_time>$b");
        $i = 0;
        $j = 1; //间隔一小时
        $time_ago =30; //取过去30小时
        do {
            $i = $i - $j;
            $array[] = [$i, $i +1];
        } while ($i >= -$time_ago);

        foreach ($array as $index => $v) {
            $timeWhere[$index] = $this->timeHours($v[0], $v[1]);
        }
//        halt($timeWhere);
        foreach ($timeWhere as $index => $item) {
            $data = Db::name('huobi')->whereTime('create_time', 'between', $timeWhere[$index])->order('create_time desc')->find();
            if (is_array($data)) {
                $arr[] = $data;
            }
        }
        foreach ($arr as $index => $item) {
            $arr[$index]['create_time'] = '"' . date('Y-m-d H:i:s', $item['create_time']) . '"';
        }
        $data = [
            'c_last' => array_column($arr, 'c_last'),
            'u_last' => array_column($arr, 'u_last'),
            'closing_price' => array_column($arr, 'closing_price'),
            'last_price' => array_column($arr, 'last_price'),
            'ltp' => array_column($arr, 'ltp'),
            'create_time' => array_column($arr, 'create_time'),
        ];
        return json($data);
    }

    public function fourHoursData()
    {
//        $a = 4;
//        $b = strtotime('-10 day'); //去小时之前
//        $arr = Db::query("
//        SELECT * FROM fa_huobi WHERE (CEILING(HOUR(FROM_UNIXTIME(create_time)) / $a) = HOUR(FROM_UNIXTIME(create_time)) / $a
//        OR FLOOR(HOUR(FROM_UNIXTIME(create_time)) / $a) = HOUR(FROM_UNIXTIME(create_time)) / $a) AND (MINUTE(FROM_UNIXTIME(create_time))=0)
//        AND  create_time>$b");

        $i = 0;
        $j = 4; //间隔4小时
        $time_ago =144; //取过去144小时
        do {
            $i = $i - $j;
            $array[] = [$i, $i +4];
        } while ($i >= -$time_ago);

        foreach ($array as $index => $v) {
            $timeWhere[$index] = $this->timeHours($v[0], $v[1]);
        }
//        halt($timeWhere);
        foreach ($timeWhere as $index => $item) {
            $data = Db::name('huobi')->whereTime('create_time', 'between', $timeWhere[$index])->order('create_time desc')->find();
            if (is_array($data)) {
                $arr[] = $data;
            }
        }


        foreach ($arr as $index => $item) {
            $arr[$index]['create_time'] = '"' . date('Y-m-d H:i:s', $item['create_time']) . '"';
        }
//        halt($arr);
//        $data = [
//            'c_last' => array_slice(array_column($arr, 'c_last'), 0, 12),
//            'u_last' => array_slice(array_column($arr, 'u_last'), 0, 12),
//            'closing_price' => array_slice(array_column($arr, 'closing_price'), 0, 12),
//            'last_price' => array_slice(array_column($arr, 'last_price'), 0, 12),
//            'ltp' => array_slice(array_column($arr, 'ltp'), 0, 12),
//        ];
        $data = [
            'c_last' => array_column($arr, 'c_last'),
            'u_last' => array_column($arr, 'u_last'),
            'closing_price' => array_column($arr, 'closing_price'),
            'last_price' => array_column($arr, 'last_price'),
            'ltp' => array_column($arr, 'ltp'),
            'create_time' => array_column($arr, 'create_time'),
        ];
        return json($data);
    }

//    public function dayHoursData()
//    {
//        $arr = Db::query("
//        ");
//        foreach ($arr as $index => $item) {
//            if (is_int($index / 8640)) {
//                $arr[$index]['create_time'] = '"' . date('H-i', $item['create_time']) . '"';
//            } else {
//                unset($arr[$index]);
//            }
//        }
//        $data = [
//            'c_last' => array_slice(array_column($arr, 'c_last'), 0, 12),
//            'u_last' => array_slice(array_column($arr, 'u_last'), 0, 12),
//            'closing_price' => array_slice(array_column($arr, 'closing_price'), 0, 12),
//            'last_price' => array_slice(array_column($arr, 'last_price'), 0, 12),
//            'ltp' => array_slice(array_column($arr, 'ltp'), 0, 12),
//        ];
//        return json($data);
//    }

    public function handelData()
    {
        /**
         * http://data.gate.io/api2/1/ticker/btc_usdt中国  取last
         * https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc美国  取Last
         * https://api.bithumb.com/public/ticker/btc 韩国    取closing_price
         * https://api.bitfinex.com/v1/pubticker/btcusd   香港  取last_price
         * https://api.bitflyer.jp/v1/ticker/ 日本   取ltp
         */
        $huilv = Http::get('https://api.fixer.io/latest?base=USD');
        $huilv_arr = \GuzzleHttp\json_decode($huilv, true);
        $c_last = Http::get('http://data.gate.io/api2/1/ticker/btc_usdt');
        $c_last_arr = json_decode($c_last, true);
        if ($c_last_arr['last'] == 0) {
            return $this->handelData();
        }
        $data['c_last'] = (double)sprintf("%1\$.2f", $c_last_arr['last']);   //中国

        $u_last = Http::get('https://bittrex.com/api/v1.1/public/getmarketsummary?market=usdt-btc');//美国
        $u_last_arr = json_decode($u_last, true);
        if ($u_last_arr['result'][0]['Last'] == 0) {
            return $this->handelData();
        }
        $data['u_last'] = (double)sprintf("%1\$.2f", $u_last_arr['result'][0]['Last']);

        $closing_pric = Http::get('https://api.bithumb.com/public/ticker/btc');//韩国
        $closing_pric_arr = json_decode($closing_pric, true);
        if ($closing_pric_arr['data']['closing_price'] == 0) {
            return $this->handelData();
        }
        $data['closing_price'] = (double)sprintf("%1\$.2f", $closing_pric_arr['data']['closing_price'] / $huilv_arr['rates']['KRW']);

        $last_price = Http::get('https://api.bitfinex.com/v1/pubticker/btcusd');//香港
        $last_price_arr = json_decode($last_price, true);
        if ($last_price_arr['last_price'] == 0) {
            return $this->handelData();
        }
        $data['last_price'] = (double)number_format($last_price_arr['last_price'], 2, '.', '');;

        $ltp = Http::get('https://api.bitflyer.jp/v1/ticker/'); // 日本
        $ltp_arr = json_decode($ltp, true);
        if ($ltp_arr['ltp'] == 0) {
            return $this->handelData();
        }
        $data['ltp'] = (double)sprintf("%1\$.2f", $ltp_arr['ltp'] / $huilv_arr['rates']['JPY']);
        $data['create_time'] = time();
        $data['type'] = input('type');
        $res = Db::name('huobi')->insert($data);
        if (!$res) {
            return json(['msg' => '保存失败', 'code' => false]);
        }
    }

    public function delData()
    {

        Db::name('huobi')->where(['create_time' => ['<', Time::daysAgo(7)]])->delete();
    }
}
